﻿using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using WebApiSelfHost.Migrations;
using WebApiSelfHost.Models;

namespace WebApiSelfHost.Contexts
{
    public class Context : DbContext
    {
        public Context()
            : base("ConnectionString")
        {
            Database.SetInitializer<Context>(new MigrateDatabaseToLatestVersion<Context, Configuration>());            
        }

        public DbSet<Employee> Employee { get; set; }
        public DbSet<Location> Location { get; set; }
        public DbSet<Skill> Skill { get; set; }
        public DbSet<EmployeeSkill> EmployeeSkill { get; set; }
        public DbSet<Level> Level { get; set; }
        public DbSet<Status> Status { get; set; }
        public DbSet<Role> Role { get; set; }
        public DbSet<Project> Project { get; set; }
        public DbSet<ProjectEmployee> ProjectEmployee { get; set; }
        public DbSet<ProjectSkill> ProjectSkill { get; set; }
        public DbSet<ProjectForcast> ProjectForcast { get; set; }
        public DbSet<Certificate> Certificate { get; set; }
        public DbSet<EmployeeCertificate> EmployeeCertificate { get; set; }
        public DbSet<Log> Log { get; set; }
        public DbSet<Notification> Notification { get; set; }
        public DbSet<EmployeeRole> EmployeeRole { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            modelBuilder.Entity<Employee>().HasRequired(e => e.Manager).WithMany().HasForeignKey(m => m.ManagerId);
            modelBuilder.Entity<ProjectEmployee>().HasRequired(pe => pe.Employee).WithMany().HasForeignKey(pe => pe.EmployeeId).WillCascadeOnDelete(false);
        }
    }
}